﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;
using System.Runtime.InteropServices;

namespace csexplorer
{
    public class HelperFunctions
    {
        public static string FormatSize(Int64 lSize)
        {
            //Format number to KB
            string stringSize = "";
            NumberFormatInfo myNfi = new NumberFormatInfo();

            Int64 lKBSize = 0;

            if (lSize < 1024)
            {
                if (lSize == 0)
                {
                    //zero byte
                    stringSize = "0";
                }
                else
                {
                    //less than 1K but not zero byte
                    stringSize = "1";
                }
            }
            else
            {
                //convert to KB
                lKBSize = lSize / 1024;
                //format number with default format
                stringSize = lKBSize.ToString("n", myNfi);
                //remove decimal
                stringSize = stringSize.Replace(".00", "");
            }

            return stringSize + " KB";
        }

        public static string FormatDate(DateTime dtDate)
        {
            //Get date and time in short format
            string stringDate =
                dtDate.ToShortDateString().ToString() + " " + dtDate.ToShortTimeString().ToString();
            return stringDate;
        }

        public static void GetIcon(
                    string fileName,
                    out System.Drawing.Icon smallIcon,
                    out System.Drawing.Icon largeIcon)
        {
            smallIcon = null;
            largeIcon = null;
            IntPtr hImgSmall;    //the handle to the system image list
            IntPtr hImgLarge;    //the handle to the system image list
            SHFILEINFO shinfo = new SHFILEINFO();

            if (System.IO.File.Exists(fileName))
            {
                hImgSmall = Win32.SHGetFileInfo(fileName, 0, ref shinfo,
                                  (uint)Marshal.SizeOf(shinfo),
                                   Win32.SHGFI_ICON |
                                   Win32.SHGFI_SMALLICON);

                smallIcon =
                       System.Drawing.Icon.FromHandle(shinfo.hIcon);


                hImgLarge = Win32.SHGetFileInfo(fileName, 0,
                                    ref shinfo, (uint)Marshal.SizeOf(shinfo),
                                    Win32.SHGFI_ICON | Win32.SHGFI_LARGEICON);

                largeIcon =
                       System.Drawing.Icon.FromHandle(shinfo.hIcon);

            }
        }
    }
}
